using Script;
using Script.UI;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ZhuMianMainView : BaseUI
{
    public Button yuJieBtn;
    public Button dongWuBtn;
    public Button shuCaiBtn;

    public Button jiangYouBtn;
    public Button weiZengBtn;
    public Button yanBtn;
    
    public Button chaShaoBtn;
    public Button haiTaiBtn;
    public Button yuMiBtn;
    public Button zhuSunBtn;
    public Button congHuaBtn;
    public Button wenQuanDanBtn;

    public Button guoBtn;
    public Button bgBtn;
    public Button lajiTongBtn;

    public Button fanhuiBtn;

    public Button guideBtn;
    public GameObject guideGo;

    public Text zhuMianText;

    private bool isStart;
    private bool guoCanClick;
    private bool tiaoWeiliaoChoose;

    //����
    private EGaoTang gaoTang;
    private ETiaoWeiLiao tiaoWeiLiao;
    private List<EPeiCai> peiCaiList;

    private float residueTime;
    public Text daoJiShi;

    public Image liDuTiao;
    private float duration = 2.0f;
    private float liDuTiaoSpeed = 1;

    private LaMianIndent lamianIndent;

    private int duiLaJiTong;

    //text
    public Text gaoTangText;
    public Text zhumianTimeText;
    public Text tiaoweiLiaoText;
    public Text peicaiText;

    protected override void OnInit(params object[] args)
    {
        BindEvent();
    }

    public void Update()
    {
        if (tiaoWeiLiao != ETiaoWeiLiao.None)
        {
            if (tiaoWeiliaoChoose)
            {
                float pingPongVal = Mathf.PingPong(Time.time / duration * liDuTiaoSpeed, 1.0f);
                liDuTiao.fillAmount = pingPongVal;
            }
        }

        if (isStart)
        {
            residueTime -= Time.deltaTime;
            if (residueTime <= 0) residueTime = 0;
            daoJiShi.text = $"{(int)residueTime}";
            zhumianTimeText.text = Constent.RuanYingDuNameDic[GetRuanyingdu((int)(15 - residueTime))];
        }
    }

    protected override void OnDispose()
    {
        UnBindEvent();
    }

    protected override void OnOpen(params object[] args)
    {
        lamianIndent = args[0] as LaMianIndent;
        isStart = false;
        guoCanClick = true;
        tiaoWeiliaoChoose = false;
        zhuMianText.text = "点击煮面";
        peiCaiList = new List<EPeiCai>();
        bgBtn.gameObject.SetActive(false);
        CleanAll();
    }

    protected override void OnClose()
    {

    }

    private void BindEvent()
    {
        yuJieBtn.onClick.AddListener(() => { AddGaoTang(EGaoTang.YuJieXiGaoTang, yuJieBtn); });
        dongWuBtn.onClick.AddListener(() => { AddGaoTang(EGaoTang.DongWuXiGaoTang, dongWuBtn); });
        shuCaiBtn.onClick.AddListener(() => { AddGaoTang(EGaoTang.ShuCaiXiGaoTang, shuCaiBtn); });

        jiangYouBtn.onClick.AddListener(() => { AddTiaoWei(ETiaoWeiLiao.JiangYou, jiangYouBtn); });
        weiZengBtn.onClick.AddListener(() => { AddTiaoWei(ETiaoWeiLiao.WeiZeng, weiZengBtn); });
        yanBtn.onClick.AddListener(() => { AddTiaoWei(ETiaoWeiLiao.Yan, yanBtn); });

        chaShaoBtn.onClick.AddListener(() => { AddPeiCai(EPeiCai.ChaShao, chaShaoBtn); });
        haiTaiBtn.onClick.AddListener(() => { AddPeiCai(EPeiCai.HaiTai, haiTaiBtn); });
        yuMiBtn.onClick.AddListener(() => { AddPeiCai(EPeiCai.YuMi, yuMiBtn); });
        zhuSunBtn.onClick.AddListener(() => { AddPeiCai(EPeiCai.ZuSun, zhuSunBtn); });
        congHuaBtn.onClick.AddListener(() => { AddPeiCai(EPeiCai.CongHua, congHuaBtn); });
        wenQuanDanBtn.onClick.AddListener(() => { AddPeiCai(EPeiCai.WenQuanDa, wenQuanDanBtn); });
        guideBtn.onClick.AddListener(OnGuideBtnClick);

        guoBtn.onClick.AddListener(CookStart);
        fanhuiBtn.onClick.AddListener(OnFanhuiBtn);
        lajiTongBtn.onClick.AddListener(OnDiuLajiTongBtnClick);
    }

    private void OnGuideBtnClick()
    {
        guideGo.SetActive(false);
    }

    private void UnBindEvent()
    {
        yuJieBtn.onClick.RemoveListener(() => { AddGaoTang(EGaoTang.YuJieXiGaoTang, yuJieBtn); });
        dongWuBtn.onClick.RemoveListener(() => { AddGaoTang(EGaoTang.DongWuXiGaoTang, dongWuBtn); });
        shuCaiBtn.onClick.RemoveListener(() => { AddGaoTang(EGaoTang.ShuCaiXiGaoTang, shuCaiBtn); });

        jiangYouBtn.onClick.RemoveListener(() => { AddTiaoWei(ETiaoWeiLiao.JiangYou, jiangYouBtn); });
        weiZengBtn.onClick.RemoveListener(() => { AddTiaoWei(ETiaoWeiLiao.WeiZeng, weiZengBtn); });
        yanBtn.onClick.RemoveListener(() => { AddTiaoWei(ETiaoWeiLiao.Yan, yanBtn); });

        chaShaoBtn.onClick.RemoveListener(() => { AddPeiCai(EPeiCai.ChaShao, chaShaoBtn); });
        haiTaiBtn.onClick.RemoveListener(() => { AddPeiCai(EPeiCai.HaiTai, haiTaiBtn); });
        yuMiBtn.onClick.RemoveListener(() => { AddPeiCai(EPeiCai.YuMi, yuMiBtn); });
        zhuSunBtn.onClick.RemoveListener(() => { AddPeiCai(EPeiCai.ZuSun, zhuSunBtn); });
        congHuaBtn.onClick.RemoveListener(() => { AddPeiCai(EPeiCai.CongHua, congHuaBtn); });
        wenQuanDanBtn.onClick.RemoveListener(() => { AddPeiCai(EPeiCai.WenQuanDa, wenQuanDanBtn); });

        guoBtn.onClick.RemoveListener(CookStart);
        fanhuiBtn.onClick.RemoveListener(OnFanhuiBtn);
        lajiTongBtn.onClick.RemoveListener(OnDiuLajiTongBtnClick);
    }


    public void CookStart()
    {
        if (guoCanClick)
        {
            zhuMianText.text = "煮面中……点击捞面";
            residueTime = 15;
            isStart = true;
            guoCanClick = false;
        }
        else
        {
            isStart = false;
            zhuMianText.text = "面已捞出";
            mianGo.transform.Find("mian").gameObject.SetActive(true);
        }
    }

    private void AddGaoTang(EGaoTang gaoTang, Button btn)
    {
        if (this.gaoTang != EGaoTang.None)
        {
            return;
        }
        this.gaoTang = gaoTang;
        gaoTangText.text = Constent.GaoTangNameDic[gaoTang];
        //ChangeBtnColor(btn);
        gaotangGo.transform.Find($"{(int)gaoTang}").gameObject.SetActive(true);
    }


    private void AddTiaoWei(ETiaoWeiLiao tiaowei, Button btn)
    {
        if (this.tiaoWeiLiao != ETiaoWeiLiao.None)
        {
            return;
        }
        this.tiaoWeiLiao = tiaowei;
        tiaoweiLiaoText.text = Constent.TiaoWeiLialDic[tiaowei];
        tiaoweiGo.transform.Find($"{(int)tiaowei}").gameObject.SetActive(true);
        tiaoWeiliaoChoose = true;
        bgBtn.gameObject.SetActive(true);
        bgBtn.onClick.AddListener(OnBgBtnClick);
    }

    private void AddPeiCai(EPeiCai peicai, Button btn)
    {
        if (peiCaiList.Count >= 6)
        {
            ToastMgr.Instance.Toast("配菜已经放满了哦");
            return;
        }
        peiCaiList.Add(peicai);
        peicaiText.text = GetPetcaisStr();
        //ChangeBtnColor(btn);
        peicaiGo.transform.Find($"{(int)peicai}").gameObject.SetActive(true);
    }

    private void OnDiuLajiTongBtnClick()
    {
        duiLaJiTong = 0;
        duiLaJiTong++;
        ResetData();
        ResetView();
    }

    private void Reset()
    {
        duiLaJiTong = 0;
        ResetData();
        ResetView();
    }

    private void ResetData()
    {
        gaoTang = EGaoTang.None;
        tiaoWeiLiao = ETiaoWeiLiao.None;
        peiCaiList = new List<EPeiCai>();
        residueTime = 15;
        daoJiShi.text = "";
        isStart = false;
        guoCanClick = true;
        tiaoWeiliaoChoose = false;
        liDuTiao.fillAmount = 0;
    }

    private void ResetView()
    {
        gaoTangText.text = string.Empty;
        zhumianTimeText.text = string.Empty;
        tiaoweiLiaoText.text = string.Empty;
        peicaiText.text = string.Empty;
        CleanAll();
    }

    private void OnDisable()
    {

    }

    public EPingJia CookEnd()
    {
        isStart = false;
        NoodlesBuilder builder = new NoodlesBuilder();

        FinishedLaMian finishedLa = builder
            .BuildGaoTang(gaoTang)
            .BuildRuanYingDu((int)(15 - residueTime))
            .BuildPeiCai(peiCaiList)
            .BuildTiaoWeiLiao(tiaoWeiLiao, liDuTiao.fillAmount)
            .Build();

        int score = LaMianMgr.Instance.MatchClientInfo(lamianIndent, finishedLa, duiLaJiTong);
        if (score < 60)
        {
            return EPingJia.goushi;
        }
        else if (score >= 60 && score < 80)
        {
            return EPingJia.bad;
        }
        else if (score >= 80 && score < 100)
        {
            return EPingJia.good;
        }
        else
        {
            return EPingJia.best;
        }
    }

    private void OnBgBtnClick()
    {
        tiaoWeiliaoChoose = false;
        bgBtn.onClick.RemoveListener(OnBgBtnClick);
        bgBtn.gameObject.SetActive(false);
    }

    private void OnFanhuiBtn()
    {
        UIMgr.Instance.OpenUI<JieDaiChuUI>(CookEnd(),mianGo.transform.parent.gameObject);
        Reset();
    }

    private ERuanYingDu GetRuanyingdu(int second)
    {
        var ruanYingDu = ERuanYingDu.MeiShu;
        if (second <= 5)
        {
            ruanYingDu = ERuanYingDu.MeiShu;
        }
        else if (second > 5 && second <= 9)
        {
            ruanYingDu = ERuanYingDu.YingMian;
        }
        else if (second > 9 && second <= 13)
        {
            ruanYingDu = ERuanYingDu.ZhongMian;
        }
        else if (second > 13 && second <= 15)
        {
            ruanYingDu = ERuanYingDu.RuanMian;
        }
        return ruanYingDu;
    }

    private string GetPetcaisStr()
    {
        string peiCaiStr = "";
        foreach (var peiCai in peiCaiList)
        {
            Debug.Log("p = " + peiCai);
            peiCaiStr += Constent.PeiCaiNameDic[(EPeiCai)peiCai] + ",";
        }
        return peiCaiStr;
    }

    public GameObject gaotangGo;
    public GameObject mianGo;
    public GameObject tiaoweiGo;
    public GameObject peicaiGo;

    private void CleanAll()
    {
        var gaotangs = gaotangGo.transform.GetComponentsInChildren<Transform>();
        foreach (var gaotang in gaotangs)
        {
            if (gaotang.gameObject != gaotangGo)
            {
                gaotang.gameObject.SetActive(false);
            }
        }
        var mian = mianGo.transform.Find("mian");
        mian.gameObject.SetActive(false);

        var tiaoweis = tiaoweiGo.transform.GetComponentsInChildren<Transform>();
        foreach (var tiaowei in tiaoweis)
        {
            if (tiaowei.gameObject != tiaoweiGo)
            {
                tiaowei.gameObject.SetActive(false);
            }
        }
        var peicais = peicaiGo.transform.GetComponentsInChildren<Transform>();
        foreach (var peicai in peicais)
        {
            if (peicai.gameObject != peicaiGo)
            {
                peicai.gameObject.SetActive(false);
            }
        }
    }
}

